/**
 *====================================================
 * 文件名称: AbnormalServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月21日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.common.util.StringUtils;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.AbnormalEntity;
import com.yuexunit.finance.project.entity.lexicon.AbnormalStateEnum;
import com.yuexunit.finance.project.entity.lexicon.HandleStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;

/**
 * @ClassName: AbnormalServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月21日 13:42:35
 */
@Implements(AbnormalService.class)
public class AbnormalServiceImpl implements AbnormalService {

	@Override
	public Long createAbnormal(AbnormalEntity entity) {
		entity.setHandleState(HandleStateEnum.WAIT_HANDLE);
		entity.setAbnormalNumber(SettingConstants.PRE_NUMBER_ABNORMAL + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getAbnormalId();
	}

	@Override
	public void updateAbnormal(AbnormalEntity entity) {
		AbnormalEntity persist = inquireAbnormal(entity.getAbnormalId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateAbnormal(AbnormalEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public AbnormalEntity inquireAbnormal(Long abnormalId) {
		return PersistUtil.get(AbnormalEntity.class, abnormalId);
	}

	@Override
	public PageList<AbnormalEntity> inquireAbnormalPageList(Long[] abnormalIds, Long enterpriseId, Long financeId, HandleStateEnum handleState, String startDateTime, String endDateTime, String financeNumber, Integer abnormalState,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalEntity.class);
		if (abnormalIds != null) {
			criteria.add(Restrictions.in("abnormalId", abnormalIds));
		}
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (handleState != null) {
			criteria.add(Restrictions.eq("handleState", handleState));
		}
		if (!StringUtils.isBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (!StringUtils.isBlank(startDateTime)) {
			criteria.add(Restrictions.gt("reportDate", startDateTime));
		}
		if (!StringUtils.isBlank(endDateTime)) {
			criteria.add(Restrictions.lt("reportDate", endDateTime));
		}
		if (abnormalState != null) {
			criteria.add(Restrictions.eq("abnormalState", AbnormalStateEnum.valueOf(abnormalState)));
		}
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countWaitHandleAbnormal() {
		DetachedCriteria criteria = DetachedCriteria.forClass(AbnormalEntity.class);
		criteria.add(Restrictions.in("handleState", new HandleStateEnum[] { HandleStateEnum.WAIT_HANDLE, HandleStateEnum.ACCEPT, HandleStateEnum.INHANDLE }));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("abnormalId")));
		return PersistUtil.get(criteria);
	}

}
